import { home, problem,blog,user } from '@/api/protobuf-loader'
import { generateProblemList } from './problems'
import { generateBlogList } from './blog'

export const generateHomeData = (email) => {
  const response = new home.HomeDataResponse()

  // 添加精选题目 - 使用统一题目数据
  const { problems } = generateProblemList(email, 0, 1, 5)
  const featuredProblems = problems.slice(0, 5)
  
  featuredProblems.forEach(p => {
    const problemInstance = new problem.Problem()
    // 确保类型与proto定义完全匹配
    problemInstance.setId(p.getId()) // 转换为int32
    problemInstance.setTitle(p.getTitle() || '')
    problemInstance.setDifficulty(p.getDifficulty())
    problemInstance.setAcceptanceRate(parseFloat((p.getAcceptanceRate() || 0.0).toFixed(2)))
    response.addFeaturedProblems(problemInstance)
  })

    // 添加精选文章 - 使用博客mock数据
  const blogData = generateBlogList({ page: 1, pageSize: 5 });
  response.setFeaturedArticlesList(blogData.blogs.map(log => {
    const blogProto = new blog.BlogPreview();
    blogProto.setId(log.id);
    blogProto.setTitle(log.title);
    blogProto.setDate(log.date);
    blogProto.setPreview(log.preview);
    blogProto.setLikes(log.likes);
    blogProto.setViewCount(log.view_count);
    const author = new user.UserStats();
    author.setEmail(log.author.email);
    author.setName(log.author.name);
    author.setAvatar(log.author.avatar);
    blogProto.setAuthor(author);
    return blogProto;
  }));

  const responsedata = response.serializeBinary();
  return responsedata;
}
